package com.xfatm.killer.task;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.xfatm.killer.R;
import com.xfatm.killer.entity.AppInfo;
import com.xfatm.killer.entity.AppInfoController;
import com.xfatm.killer.util.Activitys;

/**
 * Created by wenjie on 2018/4/3.
 */

public class StartAppTask extends AsyncTask{

    private AppInfo appInfo;
    private Activity activity;

    public StartAppTask(AppInfo appInfo, Activity activity) {
        this.appInfo = appInfo;
        this.activity = activity;
    }

    @Override
    protected Object doInBackground(Object[] objects) {
        if(!appInfo.isEnable()){
            AppInfoController.getInstance().enableAppInThread(appInfo);
        }
        publishProgress();
        return null;
    }

    @Override
    protected void onProgressUpdate(Object[] values) {
        PackageManager pm= Activitys.top().getPackageManager();
        Intent intent=pm.getLaunchIntentForPackage(appInfo.getPackageName());
        Activitys.top().startActivity(intent);
        AppInfoController.getInstance().getSelectAppInfoAdapter().notifyDataSetChanged();
        TextView textView=activity.findViewById(R.id.disableText);
        ProgressBar progressBar=activity.findViewById(R.id.disableProgress);
        textView.setVisibility(View.VISIBLE);
        progressBar.setVisibility(View.GONE);
    }
}
